package com.kuang.bx.service;

import com.kuang.bx.dao.AdminUserRoleDAO;
import com.kuang.bx.pojo.AdminRole;
import com.kuang.bx.pojo.AdminUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminUserRoleService {
    @Autowired
    AdminUserRoleDAO adminUserRoleDAO;

    public List<AdminUserRole> listAllByUid(int uid){
        return adminUserRoleDAO.findAllByUid(uid);
    }

    @Transactional
    public void saveRoleChanges(int uid, List<AdminRole> roles){
        adminUserRoleDAO.deleteByUid(uid);
        List<AdminUserRole> urs = new ArrayList<>();
        roles.forEach(r->{
            AdminUserRole ur = new AdminUserRole();
            ur.setUid(uid);
            urs.add(ur);
        });
        adminUserRoleDAO.saveAll(urs);

    }
}
